package com.outdoor.model.dto.response;

import com.outdoor.model.entity.Team;
import com.outdoor.model.entity.User;
import com.outdoor.model.enums.ActivityType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

/**
 * 团队响应DTO
 * 
 * @author OutdoorTeam
 * @date 2025-05-18
 */
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class TeamResponse {

    private Long id;
    private String name;
    private ActivityType activityType;
    private String location;
    private LocalDate date;
    private Integer maxMembers;
    private String description;
    private String experienceLevel;
    private String additionalRequirements;
    
    private LeaderInfo leader;
    private Integer currentMembers;
    private Integer pendingApplications;
    private boolean isFull;
    
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class LeaderInfo {
        private Long id;
        private String name;
        private Integer tripsCount;
    }
    
    /**
     * 从实体转换为DTO (基础版本，不含关联信息)
     * 
     * @param team 团队实体
     * @return 团队响应DTO
     */
    public static TeamResponse fromEntity(Team team) {
        TeamResponse.TeamResponseBuilder builder = TeamResponse.builder()
                .id(team.getId())
                .name(team.getName())
                .activityType(team.getActivityType())
                .location(team.getLocation())
                .date(team.getDate())
                .maxMembers(team.getMaxMembers())
                .description(team.getDescription())
                .experienceLevel(team.getExperienceLevel())
                .additionalRequirements(team.getAdditionalRequirements());
                
        return builder.build();
    }
    
    /**
     * 从实体转换为DTO (含领队信息)
     * 
     * @param team 团队实体
     * @param leaderName 领队名称
     * @param tripsCount 领队行程数
     * @return 团队响应DTO
     */
    public static TeamResponse fromEntity(Team team, String leaderName, Integer tripsCount) {
        return fromEntity(team).toBuilder()
                .leader(LeaderInfo.builder()
                        .id(team.getLeaderId())
                        .name(leaderName)
                        .tripsCount(tripsCount)
                        .build())
                .build();
    }
    
    /**
     * 使用额外信息补充TeamResponse
     * 
     * @param team 团队实体
     * @param leader 领队信息
     * @param memberCount 成员数量
     * @param pendingCount 待处理申请数量
     * @return 完整的团队响应DTO
     */
    public static TeamResponse fromEntityWithDetails(Team team, User leader, int memberCount, int pendingCount) {
        return fromEntity(team).toBuilder()
                .leader(LeaderInfo.builder()
                        .id(leader.getId())
                        .name(leader.getName())
                        .tripsCount(leader.getTripsCount())
                        .build())
                .currentMembers(memberCount)
                .pendingApplications(pendingCount)
                .isFull(memberCount >= team.getMaxMembers())
                .build();
    }
    
    /**
     * 从实体集合转换为DTO列表
     * 
     * @param teams 团队实体集合
     * @return 团队响应DTO列表
     */
    public static List<TeamResponse> fromEntityList(List<Team> teams) {
        if (teams == null) {
            return Collections.emptyList();
        }
        return teams.stream()
                .map(TeamResponse::fromEntity)
                .toList();
    }
} 